package org.zgy.stock.threads;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.zgy.stock.model.HoldOrgInfo;
import org.zgy.stock.service.HoldOrgInfoService;
import org.zgy.stock.utils.okhttp3.OkHttpUtil;

public class HoldOrgInfoThread implements Runnable {
    private final static String API_URL = "https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_PUBLIC_OP_HOLDINGORG&columns=ALL&quoteColumns=&filter=(SECUCODE%3D%22STOCK_CODE_WITH_MARKET%22)&pageNumber=1&pageSize=1000&sortTypes=&sortColumns=&source=HSF10&client=PC&v=04556322507711166";
    private final String code;
    private final HoldOrgInfoService holdOrgInfoService;

    public HoldOrgInfoThread(String code, HoldOrgInfoService holdOrgInfoService) {
        this.code = code;
        this.holdOrgInfoService = holdOrgInfoService;
    }

    @Override
    public void run() {
        String stock_code = "";
        if (code.startsWith("6")) {
            stock_code = code + ".SH";
        } else {
            stock_code = code + ".SZ";
        }
        String result = OkHttpUtil.getRequest(API_URL.replace("STOCK_CODE_WITH_MARKET", stock_code), null);
//        System.out.println(JSON.parseObject(result));
        DfcfResponse<HoldOrgInfo> response = JSON.parseObject(
                result,
                new TypeReference<DfcfResponse<HoldOrgInfo>>() {
                }
        );

        if (response.isSuccess()) {
//            System.out.println(response.getResult().getData());
            holdOrgInfoService.insert(response.getResult().getData());
        } else {
            System.err.println("请求失败: " + code + " : " + response.getMessage());
        }
    }
}
